package yxy.game.pm2.module.guard;

import java.util.ArrayList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import yxy.cherry.battle.module.Position;
import yxy.game.pm2.bean.Format.FormatType;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.battle.FighterModule;
import yxy.game.pm2.module.battle.FighterModule.InitHpResidual;
import yxy.game.pm2.module.battle.TeamModule;
import yxy.game.pm2.module.battle.WeaponModule;

public class Guard {
	// bean===========================================================================

	private String playerCode;// 守将code
	private ArrayList<GuardHero> heros = new ArrayList<>();// 武将快照
	private WeaponModule weapon;// 神器快照
	private long power;// 战力

	public String getPlayerCode() {
		return playerCode;
	}

	public void setPlayerCode(String playerCode) {
		this.playerCode = playerCode;
	}

	public ArrayList<GuardHero> getHeros() {
		return heros;
	}

	public void setHeros(ArrayList<GuardHero> heros) {
		this.heros = heros;
	}

	public WeaponModule getWeapon() {
		return weapon;
	}

	public void setWeapon(WeaponModule weapon) {
		this.weapon = weapon;
	}

	public long getPower() {
		return power;
	}

	public void setPower(long power) {
		this.power = power;
	}

	// func===========================================================================
	public boolean alive() {
		for (GuardHero hero : getHeros()) {
			if (hero.getResidual() > 0) {
				return true;
			}
		}
		return false;
	}

	public int hp() {
		Double residual = 0.0;
		Double hp = 0.0;
		for (GuardHero hero : getHeros()) {
			if (hero.getResidual() > 0) {
				residual += hero.getResidual();
			}
			hp += hero.getHp();
		}
		residual = residual * 10000 / hp;
		int value = residual.intValue();
		return value < 0 ? 0 : (value > 10000 ? 10000 : value);
	}

	public TeamModule team() {
		TeamModule team = new TeamModule();
		// 武将
		for (GuardHero hero : getHeros()) {
			FighterModule fighterModule = hero.getFighterModule();
			fighterModule.setInitHp(new InitHpResidual(hero.getHp(), hero.getResidual()));
			team.addfighter(hero.getPos(), fighterModule);
		}
		// 神器
		team.setWeapon(getWeapon());

		PlayerBean playerBean = PlayerBean.get(getPlayerCode());
		team.setName(playerBean.getName());
		team.setHead(playerBean.getHead());
		team.setLevel(playerBean.getLevel());
		team.setPlayerCode(playerBean.getCode());
		team.setPower(getPower());

		return team;
	}

	public static Guard create(String playerCode) {
		PlayerBean playerBean = PlayerBean.get(playerCode);
		if (playerBean == null) {
			return null;
		}
		Guard guard = new Guard();
		guard.setPlayerCode(playerCode);

		TeamModule team = new BattleHandle().teamPlayer(playerCode, FormatType.普通);

		// 武将
		Map<Position, FighterModule> fighters = team.fighters();
		Set<Entry<Position, FighterModule>> entrySet = fighters.entrySet();
		for (Entry<Position, FighterModule> entry : entrySet) {
			Position position = entry.getKey();
			FighterModule fighterModule = entry.getValue();
			fighterModule.getProperty().setLife(fighterModule.getProperty().getLife() * 60);
			long hp = fighterModule.hp();
			GuardHero hero = new GuardHero();
			hero.setPos(position);
			hero.setFighterModule(fighterModule);
			hero.setHp(hp);
			hero.setResidual(hp);

			guard.getHeros().add(hero);
		}
		guard.setWeapon(team.getWeapon());

		guard.setPower(team.power());
		return guard;
	}

	/** 加血操作 */
	public void addHp(int add) {
		for (GuardHero hero : heros) {
			long resi = hero.getResidual();
			long hp = hero.getHp();
			if (resi < 0) {
				resi = 0;
			}
			resi += hp * add / 10000;
			if (resi > hp) {
				resi = hp;
			}
			hero.setResidual(resi);
		}
	}

}
